﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BusinessObject;

namespace YamahaTuanTrucService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "StatisticService" in code, svc and config file together.
    public class StatisticService : IStatisticService
    {

        public SuaChua[] GetListBusByMonth(string month, string year)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                int m = Convert.ToInt32(month);
                int y = Convert.ToInt32(year);
                //List<SuaChua> lst = null;
                try
                {
                    var query = from suachua in context.SuaChua.Include("Xe")
                                where suachua.Xe.NgayNhanXe.Month == m &&
                                suachua.Xe.NgayNhanXe.Year == y
                                select suachua;
                    return query.ToArray<SuaChua>();
                }
                catch (FaultException e)
                {
                    Console.WriteLine(e.Reason);
                    return null;
                }
            }

        }


        public XuatHang[] GetListPartByMonth(string month, string year)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                int m = Convert.ToInt32(month);
                int y = Convert.ToInt32(year);
                try
                {
                    var query = from xuathang in context.XuatHang
                                where xuathang.NgayXuat.Month == m &&
                                xuathang.NgayXuat.Year == y
                                select xuathang;
                    return query.ToArray<XuatHang>();
                }
                catch
                {
                    return null;
                }
            }
        }

        public KhachHang[] GetListCustomerByMonth(string month, string year)
        {
            int m = Convert.ToInt32(month);
            int y = Convert.ToInt32(year);
            using (var context = new YamahaTuanTrucEntities())
            {
                var query = from kh in context.KhachHang.Include("Xe")
                            where kh.Xe.Any(x => x.NgayNhanXe.Month == m
                                && x.NgayNhanXe.Year == y)
                            select kh;
                return query.ToArray<KhachHang>();
            }
        }

        public SuaChua[] GetListBusByQuarter(int quarter, string year)
        {
            int startmonth = 0;
            int endmonth = 0;
            if(quarter == 0)
            {
                startmonth = 1;
                endmonth = 3;
            }
            else if(quarter == 1)
            {
                startmonth = 4;
                endmonth = 6;
            }
            else if(quarter == 2)
            {
                startmonth = 7;
                endmonth = 9;
            }
            else if(quarter == 3)
            {
                startmonth = 10;
                endmonth = 12;
            }
            int y = Convert.ToInt32(year);
            using (var context = new YamahaTuanTrucEntities())
            {
                //List<SuaChua> lst = null;
                try
                {
                    var query = from suachua in context.SuaChua.Include("Xe")
                                where (suachua.Xe.NgayNhanXe.Month >= startmonth && suachua.Xe.NgayNhanXe.Month <= endmonth)
                                && suachua.Xe.NgayNhanXe.Year == y
                                select suachua;
                    return query.ToArray<SuaChua>();
                }
                catch(Exception e)
                {
                    return null;
                }
            }
        }
    }
}
